/**
 * Copyright (C), 2015-2019, XXX有限公司
 * FileName: InterceptorHandler
 * Author:   Administrator
 * Date:     2019/12/20 23:17
 * Description:
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.lumeng.spring.common.handler;

import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 功能描述:
 * <>
 *
 * @program: etc_issue
 * @ClassName: InterceptorHandler
 * @author: wanghui
 * @date: 2019/12/20 23:17
 * @Version: 1.0.0
 *
 * @Copyright: 2019 Inc. All rights reserved. 注意: 本内容仅限于内部传阅,禁止外泄以及用于其他的商业目
 */
public class InterceptorHandler implements LoggerInterceptorHandler{
    private static final Logger logger= LoggerFactory.getLogger(InterceptorHandler.class);

    private String wrapJson(final Object obj,final int maxLength) {
        if (obj == null) {
            return "";
        }
        String json= JSON.toJSONString(obj);
        if (json == null) {
            return "";
        }
        if (json.length() > maxLength) {
            json = json.substring(0, maxLength - 1);
        }
        return json;
    }

    @Override
    public void interptThrowing(Long traceId,String remoteIp,String localIp,String description,String targetName,String methodName, Throwable ex){
        StringBuilder stringBuilder = new StringBuilder();
        if (traceId!=null)
            stringBuilder.append("[").append(traceId).append("]");
        if (StringUtils.isNotEmpty(remoteIp))
            stringBuilder.append(",目标IP:").append(remoteIp);
        if (StringUtils.isNotEmpty(localIp))
            stringBuilder.append(",本机IP:").append(localIp);

        stringBuilder.append(",当前对象类class:").append(targetName)
                .append(",调用方法method:").append(methodName);
        logger.error(stringBuilder.toString(),ex);
        stringBuilder.setLength(0);
    }

    @Override
    public void interptBefore(Long traceId,String remoteIp,String localIp,String description,String targetName,String methodName, Object args){
        StringBuilder stringBuilder = new StringBuilder();
        if (traceId!=null)
            stringBuilder.append("[").append(traceId).append("]");
        if (StringUtils.isNotEmpty(remoteIp))
            stringBuilder.append(",目标IP:").append(remoteIp);
        if (StringUtils.isNotEmpty(localIp))
            stringBuilder.append(",本机IP:").append(localIp);

        stringBuilder.append(",当前对象类class:").append(targetName)
                .append(",调用方法method:").append(methodName)
                .append("-->{");
        stringBuilder.append("\n    参数:"+this.wrapJson(args,400));
        stringBuilder.append("\n}");
        logger.info(stringBuilder.toString());
        stringBuilder.setLength(0);
    }

    @Override
    public void interceptPost(Long traceId,String remoteIp,String localIp,String description,String targetName,String methodName, Object result) {
        StringBuilder stringBuilder = new StringBuilder();
        if (traceId!=null)
            stringBuilder.append("[").append(traceId).append("]");
        if (StringUtils.isNotEmpty(remoteIp))
            stringBuilder.append(",目标IP:").append(remoteIp);
        if (StringUtils.isNotEmpty(localIp))
            stringBuilder.append(",本机IP:").append(localIp);

        stringBuilder.append(",当前对象类class:").append(targetName)
                .append(",调用方法method:").append(methodName)
                .append("-->{");
        stringBuilder.append("\n    结果:" + this.wrapJson(result, 400));
        stringBuilder.append("\n}");
        logger.info(stringBuilder.toString());
        stringBuilder.setLength(0);
    }

    @Override
    public void interptFinally(Long traceId,String remoteIp,String localIp,String description,String targetName,String methodName, long startTime){
        StringBuilder stringBuilder = new StringBuilder();
        long endTime = System.currentTimeMillis();
        if (traceId!=null)
            stringBuilder.append("[").append(traceId).append("]");
        if (StringUtils.isNotEmpty(remoteIp))
            stringBuilder.append(",目标IP:").append(remoteIp);
        if (StringUtils.isNotEmpty(localIp))
            stringBuilder.append(",本机IP:").append(localIp);

        stringBuilder.append(",当前对象类class:").append(targetName)
                .append(",调用方法method:").append(methodName);
        if(startTime > 0){
            stringBuilder.append("-->{")
                    .append("耗时:")
                    .append(endTime-startTime)
                    .append("毫秒")
                    .append("}");
        }

        logger.info(stringBuilder.toString());
        stringBuilder.setLength(0);
    }
}
